<?php

/**
 * Description of VirtualLabs_Rest_Network_Network
 *
 * @author markos
 */
class VirtualLabs_Rest_Network_Network {

    const NETWORK_OFFERING_ID = '';
    const ERROR_CODE_MAX_NETWORK = 534;

//    [errorcode] => 534
//            [errortext] => Maximum number of resources of type 'network' for project name=virtual-labs-lab-1 in domain id=1 has been exceeded.
//    [errorcode] => 431
//            [errortext] => Invalid netmask

    private $zoneId;
    private $debugLog;
    private $networkOfferingId;

    public function __construct() {
        $this->debugLog = Zend_Registry::get(VirtualLabs_Common::DEBUG_LOG);
        $config = new Zend_Config_Ini(CLOUDSTACK_CONFIG_FILE, APPLICATION_ENV);
        $this->zoneId = $config->get('zoneid');
        $this->networkOfferingId = $config->get('networkofferingid');
    }

    /**
     * Crea una red para un laboratorio.
     * 
     * @param type $displayText
     * @param type $name
     * @param type $networkOfferingId
     * @param type $projectId
     * @param type $zoneId
     * @param type $accountId
     * @return type
     */
    public function createNetwork($displayText, $name, $projectId, $accountId, $gateway, $netmask) {
        $url = array();
        $this->debugLog->debug(" [ createNetwork ] dt $displayText na $name pr $projectId ac $accountId gw $gateway nm $netmask ");
        $url["response"] = "json";
        $url["command"] = "createNetwork";
        $url["displaytext"] = $displayText;
        $url["name"] = $name;
        $url["networkofferingid"] = $this->networkOfferingId;
        $url["projectid"] = $projectId;
        $url["zoneid"] = $this->zoneId;
        $url["accountid"] = $accountId;
        $url['gateway'] = $gateway;
        $url['netmask'] = $netmask;
        $v = new VirtualLabs_Rest_HttpClient_Client();
        $response = $v->call($url);

        $createNetworkResponse = Zend_Json_Decoder::decode($response);
        return $createNetworkResponse;
    }

    public function listNetworks($projectId, $accountId, $networkId = null) {
        $url = array();
        $url["response"] = "json";
        $url["command"] = "listNetworks";
        if ($networkId)
            $url["id"] = $networkId;
        $url["projectid"] = $projectId;
        $url["zoneid"] = $this->zoneId;
        $url["account"] = $accountId;
        $v = new VirtualLabs_Rest_HttpClient_Client();
        $response = $v->call($url);

        $createNetworkResponse = Zend_Json_Decoder::decode($response);
        return $createNetworkResponse;
    }

    public function destroyNetwork($networkId) {
        $url = array();
        $url["response"] = "json";
        $url["command"] = "deleteNetwork";
        $url["id"] = $networkId;
        $v = new VirtualLabs_Rest_HttpClient_Client();
        $response = $v->call($url);

        $createNetworkResponse = Zend_Json_Decoder::decode($response);
        return $createNetworkResponse;
    }

}

?>
